package com.shanzi.recycleviewtest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;

import it.gmariotti.recyclerview.itemanimator.SlideInOutLeftItemAnimator;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRcv_show;
    private MyAdapter mAdapter;
    private ArrayList<String> mMData;

    private Button btn_insert;
    private Button btn_remove;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAdapter = new MyAdapter(MainActivity.this, initData());
        mRcv_show = (RecyclerView) findViewById(R.id.rcv_show);
        btn_insert = (Button) findViewById(R.id.btn_insert);
        btn_remove = (Button) findViewById(R.id.btn_remove);

        btn_insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.notifyItemInserted(2);
            }
        });

        btn_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.notifyItemRemoved(3);
            }
        });

        //布局
//      mRcv_show.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
//      mRcv_show.setLayoutManager(new GridLayoutManager(this, 4));
        mRcv_show.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));

        //动画
//      mRcv_show.setItemAnimator(new DefaultItemAnimator());

        //GitHub动画
        mRcv_show.setItemAnimator(new SlideInOutLeftItemAnimator(mRcv_show));

        //分割线
//      mRcv_show.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL_LIST));
        mRcv_show.addItemDecoration(new DividerGridItemDecoration(MainActivity.this));

        mRcv_show.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MainActivity.this, "click" + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {

                Toast.makeText(MainActivity.this, "longClick" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private ArrayList<String> initData() {
        mMData = new ArrayList<String>();
        for (int i = 1; i < 100; i++) {
            String str = "";
            if (i < 10) {
                str = "0" + i;
            } else {
                str = "" + i;
            }
            mMData.add(str);
        }
        return mMData;
    }
}
